﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using Small.Util.Reflection;

namespace Small.Html.UI
{
   public class BaseHelper:BaseExpress
    {

     /// <summary>
     /// 获取到匿名类的属性
     /// </summary>
     /// <param name="htmlAttributes">匿名类</param>
        /// <returns>AnonymousInfo</returns>
       
       public static IEnumerable<AnonymousInfo> GetHtmlAttributes(object htmlAttributes) 
       {
           return AnonymousProperties.GetAnonymousInfomation(htmlAttributes);
       }

       /// <summary>
       /// 格式化属性配置
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="value"></param>
       /// <returns>name="value"值</returns>
       public static string FormatGenerateAttribute(string name,object value)
       {

           return string.Format(" {0}=\"{1}\"",name,value);
       }
       /// <summary>
       /// 格式化属性节点
       /// </summary>
       /// <param name="htmlAttributes">匿名类</param>
       /// <returns>string  返回格式化[attr="A" attr1="B"]</returns>
       public static string FormatGenerateAttribute(object htmlAttributes)
       {

           if (htmlAttributes == null) 
           {
               return null;
           }
           StringBuilder builder=new StringBuilder();
           var attributes = BaseHelper.GetHtmlAttributes(htmlAttributes);

           foreach (var item in attributes)
           {
              builder .Append(FormatGenerateAttribute(item.Name, item.Value));
           }

           return builder.ToString();
       }

       public static string SubSymbal(StringBuilder builder)
       {
           return builder.ToString().Trim(',');
       }


       public static List<String> GetDisplayFields<T>(List<Expression<Func<T, object>>> fields)
       {
           List<String> list = new List<string>();
           foreach (var item in fields)
           {
               var name = ExpressionHelper.GetExpressionText(item);
               list.Add(name);
           }
           return list;
       }

       public static string ComvertToName<T, M>(Expression<Func<T, M>> express) 
       {
           return ExpressionHelper.GetExpressionText(express);
       }
    }
}
